<?php

/**
 * 后台登录控制器父类
 */
class LoginController extends Yaf_Controller_Abstract
{

    public function init()
    {
        //设置新模板路径
        $this->setViewpath(APPLICATION_PATH . '/application/modules/Admin/views');
        $this->initView();

        $this->_user = new AdminModel();
        $this->_config = Yaf_Registry::get('config');
        if($this->_config->toArray()['log']){
	        $this->_log = new LogModel();        	
        }
    }

    public function inAction()
    {
        $this->_view->assign("title", '登录');       
        if ($this->getRequest()->isPost()) {
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('username')->minLength(4)->maxLength(16);
            $validator->field('password')->minLength(4)->maxLength(16);
            
            if($validator->validate())
            {
                $username = $this->getRequest()->getPost('username');
                $password = $this->getRequest()->getPost('password');
                $password = sha1($password . Yaf_Registry::get('config')->application['salt']);
                $return = $this->_user->checklogin($username, $password);
                if ($return) {
                    $this->clearsession(); //清理session以防万一
                    Yaf_Session::getInstance()->set("admin_username", $username);
                    Yaf_Session::getInstance()->set("admin_uid", $return[0]);
                    Yaf_Session::getInstance()->set("admin_roleid", $return[1]);
                    //记录登录日志
                    if($this->_config->toArray()['log']){
                    	$info['operator'] = $username;
                    	$info['operate'] = '登录';
                    	$info['uri'] = '/admin/lonin/in';
                    	$info['ip'] = getremoteip();
	                    $this->_log->insert($info);                    	
                    }
                    $returnarray = array('status' => '1', 'data' => '登录成功，即将进入系统' );
                    exit ( json_encode($returnarray) );
                } else {
                    $returnarray = array('status' => '0', 'data' => '登录失败，用户名或密码不正确' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        $this->_view->display('Public/login');
    }
    
    public function outAction()
    {	
    	//判断是否是超级管理员退出一键登录
        if(Yaf_Session::getInstance()->get('super')){
        	$super = Yaf_Session::getInstance()->get('super');
        	Yaf_Session::getInstance()->set('admin_username',$super['admin_username']);
        	Yaf_Session::getInstance()->set('admin_uid',$super['admin_uid']);
        	Yaf_Session::getInstance()->set('admin_roleid',$super['admin_roleid']);
        	Yaf_Session::getInstance()->del("super");
        	Yaf_Session::getInstance()->del('admin_menu');
        	//记录退出一键登录日志
        	if($this->_config->toArray()['log']){
        		$info['operator'] = $super['admin_username'];
        		$info['operate'] = '退出一键登录';
        		$info['uri'] = '/admin/lonin/out';
        		$info['ip'] = getremoteip();
        		$this->_log->insert($info);
        	}
			header('Location:/admin/user/list');
        }else{
        	//记录登出日志
        	if($this->_config->toArray()['log']){
        		$info['operator'] = Yaf_Session::getInstance()->get("admin_username");
        		$info['operate'] = '登出';
        		$info['uri'] = '/admin/lonin/out';
        		$info['ip'] = getremoteip();
        		$this->_log->insert($info);
        	}
	    	$this->clearsession();
	        header('Location:/admin/login/in');        	
        }
    }
    
    // 超级管理员一键登录 author wuzhenhao 2015-10-16
    public function onekloginAction(){
    	if(!Yaf_Session::getInstance()->get('admin_username')){
    		header('Location:/admin/login/in');
    		exit();
    	}    	
		$id = $this->getRequest()->get('id');
		if(intval($id)===1){
			header('Location:/admin/user/list');
		}else{
			$return = $this->_user->find(intval($id));	
			if($return->status){
				$super['admin_username'] = Yaf_Session::getInstance()->get('admin_username');
				$super['admin_uid'] = Yaf_Session::getInstance()->get('admin_uid');
				$super['admin_roleid'] = Yaf_Session::getInstance()->get('admin_roleid');
				Yaf_Session::getInstance()->set('super',$super);
				Yaf_Session::getInstance()->set('admin_username',$return->username);
				Yaf_Session::getInstance()->set('admin_uid',$return->id);
				Yaf_Session::getInstance()->set('admin_roleid',$return->roleid);
				Yaf_Session::getInstance()->del('admin_menu');
				//记录一键登录日志
				if($this->_config->toArray()['log']){
					$info['operator'] = $super['admin_username'];
					$info['operate'] = '一键登录';
					$info['uri'] = '/admin/lonin/oneklogin';
					$info['ip'] = getremoteip();
					$this->_log->insert($info);
				}
				header('Location:/admin/index/index');
			}else{
				header('Location:/admin/user/list');
			}		
		}
    }
    
    private function clearsession()
    {
        Yaf_Session::getInstance()->del("admin_username");
        Yaf_Session::getInstance()->del("admin_uid");
        Yaf_Session::getInstance()->del("admin_menu");
        Yaf_Session::getInstance()->del("admin_roleid");
        Yaf_Session::getInstance()->del("super");
    }
}